<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: best_sellers.php 140 2013-09-22 07:44:23Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 140 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:44:23 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

if(!$product->isProduct()) {
	$box_smarty = new smarty();
	$box_content = '';

	$box_smarty->assign('language', $_SESSION['language']);

	if(!CacheCheck() || !FORCE_CACHE) {
	 	$cache=false;
		$box_smarty->caching = false;
	} else {
		$cache=true;
		$box_smarty->caching = true;
		$box_smarty->cache_lifetime = CACHE_LIFETIME;
		$box_smarty->cache_modified_check = CACHE_CHECK;
		$cache_id = $_SESSION['language'].$current_category_id;
	}

	if(!$box_smarty->isCached(CURRENT_TEMPLATE.'/boxes/box_best_sellers.html', $cache_id) || !$cache) {
		$box_smarty->assign('tpl_path', 'templates/'.CURRENT_TEMPLATE.'/');
		$box_smarty->assign('html_tpl_path', CURRENT_TEMPLATE.'/html');

		require_once (DIR_FS_INC.'inc.row_number_format.php');

		if ($_SESSION['customers_status']['customers_fsk18_display'] == '0')
			$fsk_lock = ' and p.products_fsk18!=1';

		if(GROUP_CHECK == 'true')
			$group_check = " AND p.group_permission_".$_SESSION['customers_status']['customers_status_id']." = 1 ";
		
		$list_data = get_list_data('best_sellers');
		
		if(isset($current_category_id) && ($current_category_id > 0)) {
			$best_sellers = $db->db_query_limit("SELECT
													p.products_id,
													p.products_price,
													p.products_tax_class_id,
													p.products_image,
													p.products_vpe,
													p.products_vpe_status,
													p.products_vpe_value,
													pd.products_name
												FROM
													".TABLE_PRODUCTS." p,
													".TABLE_PRODUCTS_DESCRIPTION." pd,
													".TABLE_PRODUCTS_TO_CATEGORIES." p2c,
													".TABLE_CATEGORIES." c
												WHERE
													p.products_status = '1'
												AND
													c.categories_status = '1'
												AND
													p.products_ordered > 0
												AND
													p.products_id = pd.products_id
												AND
													pd.language_id = '".(int) $_SESSION['languages_id']."'
												AND
													p.products_id = p2c.products_id
													".$group_check."
													".$fsk_lock."
												AND
													p2c.categories_id = c.categories_id
												AND
													'".$current_category_id."' IN (c.categories_id, c.parent_id)
												GROUP BY
														p.products_id
												ORDER BY
													p.products_ordered DESC", $list_data['box_count']);
		} else {
			$best_sellers = $db->db_query_limit("SELECT
														p.products_id,
														p.products_image,
														p.products_price,
														p.products_vpe,
														p.products_vpe_status,
														p.products_vpe_value,
														p.products_tax_class_id,
														pd.products_name
													FROM
														".TABLE_PRODUCTS." p, 
														".TABLE_PRODUCTS_DESCRIPTION." pd
													WHERE
														p.products_status = '1'
														".$group_check."
													AND
														p.products_ordered > 0
													AND
														p.products_id = pd.products_id ".$fsk_lock."
													AND
														pd.language_id = '".(int) $_SESSION['languages_id']."'
													ORDER BY
														p.products_ordered DESC", $list_data['box_count']);
		}

		if($best_sellers->_numOfRows >= MIN_DISPLAY_BESTSELLERS) {
			$rows = 0;
			$box_bestseller_content = array ();
			while(!$best_sellers->EOF) { 
				$rows ++;
				$best_sellers->fields = array_merge($best_sellers->fields, array ('ID' => row_number_format($rows)));
				$box_bestseller_content[] = $product->buildDataArray($best_sellers->fields, 'thumbnail', $list_data, $rows);
				$best_sellers->MoveNext();
			}
			$box_smarty->assign('box_bestseller_content', $box_bestseller_content);
			if($list_data['box_effect'] != 'none') {
				$get[] = 'bs='.$list_data['box_effect'];
				$get[] = 'bsd='.$list_data['effect_direction'];
			}
		}

		 if (!$cache) {
		 	if ($box_bestseller_content!='')
				$box_content = $box_smarty->fetch(CURRENT_TEMPLATE.'/boxes/box_best_sellers.html');
		} else
			$box_content = $box_smarty->fetch(CURRENT_TEMPLATE.'/boxes/box_best_sellers.html', $cache_id);
	}
	$smarty->assign('bestseller', $box_content);
}
?>